import { useState, useRef, useEffect } from 'react';

const useSetState = initState => {
  const [state, __setState] = useState(initState);
  const callback = useRef();
  const setState = (state, cb) => {
    callback.current = cb;
    __setState(state);
  };

  useEffect(() => {
    if (callback.current) {
      callback.current();
      callback.current = null;
    }
  }, [state]);

  return [state, setState];
};

export default useSetState;
